package com.hopu.community_task.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hopu.community_task.dao.TaskDao;
import com.hopu.community_task.dao.UserinfoDao;
import com.hopu.community_task.domain.PageBean;
import com.hopu.community_task.domain.Task;
import com.hopu.community_task.domain.Userinfo;
import com.hopu.community_task.util.DBUtil;
/**
 * 任务表dao层实现类
 * @author slddls
 * 
 */
public class TaskDaoImpl implements TaskDao {

	private PreparedStatement pst;
	private ResultSet rs;
	@Override
	public int save(Task task) {
		String sql = "insert into task(content,start_time,"
				+ "start_uid,state,t_img,task_money,tname,start_user_tel)  "
				+ "values(?,?,?,?,?,?,?,?)";
		pst=DBUtil.getPst(sql);
		int num=0;
		try {
			pst.setString(1, task.getContent());
			pst.setString(2, task.getStart_time());
			pst.setInt(3, task.getStart_user().getUid());
			pst.setString(4, "0");
			pst.setString(5, task.getT_img());
			pst.setInt(6, task.getTask_money());
			pst.setString(7, task.getTname());
			pst.setString(8, task.getStart_user_tel());
			num = pst.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		if(num>0){
			num=findByT_img(task);
		}
		return num;
	}

	@Override
	public int update(Task task) {
		String sql = "update task set content=?,start_time=?"
				+ ",start_uid=?,state=?,t_img=?,task_money=?,"
				+ "tname=?,start_user_tel=?  where tid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, task.getContent());
			pst.setString(2, task.getStart_time());
			pst.setInt(3, task.getStart_user().getUid());
			pst.setString(4, "0");
			pst.setString(5, task.getT_img());
			pst.setInt(6, task.getTask_money());
			pst.setString(7, task.getTname());
			pst.setString(8, task.getStart_user_tel());
			pst.setInt(9, task.getTid());
			int num = pst.executeUpdate();
			return num;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return 0;
	}

	@Override
	public int remove(int tid) {
		String sql = "delete from task where tid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setInt(1, tid);
			int num = pst.executeUpdate();
			return num;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return 0;
	}

//	@Override
//	public Task findById(int tid) {
//		  //                    1    2       3           4            5         6       7           8
//		String sql = "select t.tid,t.tname,t.content,t.start_uid,t.start_time,t.state,t.end_uid,t.end_time,"
//				    // 9           10    11				12
//				+ "t.contact,t.t_img,t.start_user_tel,t.end_user_tel,	"
//				//   13			14			15		16			17
//				+ "u1.uname ,u1.realname,u1.sex,u1.identity,u1.good_value,"
//				//  18		  19		20		21		
//				+ "u1.money,u1.qq,u1.weixin,u1.tel,"
//				//  22		23			24
//				+ "u2.uname,u2.realname,u2.sex,"
//				// 25 			26				27		28		29		30		31
//				+ "u2.identity,u2.good_value,u2.money,u2.qq,u2.weixin,u2.tel,t.task_money  "
//				+ "from task t inner JOIN userinfo u1 on u1.uid=t.start_uid "
//				+ "   left join userinfo u2 on u2.uid= t.end_uid "
//				+ "where tid=?";
//		pst=DBUtil.getPst(sql);
//		try {
//			pst.setInt(1, tid);
//			rs=pst.executeQuery();
//			if(rs.next()){
//				// 任务对象
//				Task task  = new Task(rs.getInt(1), rs.getString(2), 
//						rs.getString(3), null, 
//						rs.getString(5), rs.getString(6), 
//						null, rs.getInt(31), 
//						rs.getString(8), rs.getString(10));
//				// 封装 发布任务的用户
//				Userinfo u = new Userinfo(rs.getInt(4), rs.getString(13), null, rs.getString(14), rs.getString(15),
//						rs.getString(16), rs.getString(19), rs.getString(20), rs.getString(21), rs.getInt(17),
//						rs.getInt(18));
//				task.setStart_user(u);
//				// 封装接取任务的用户
//				u = new Userinfo(rs.getInt(7), rs.getString(22), null, rs.getString(23), rs.getString(24),
//						rs.getString(25), rs.getString(28), rs.getString(29), rs.getString(30), rs.getInt(26),
//						rs.getInt(27));
//				task.setEnd_user(u);
//				return task;
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}finally{
//			DBUtil.close(rs);
//		}
//		return null;
//	}

	@Override
	public List<Task> likeFind(String str) {
		String sql = "select t.* from task t where tname like ? or content like ? "
				+ "order by state, task_money desc,start_time desc";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, "%"+str+"%");
			pst.setString(2, "%"+str+"%");
			rs=pst.executeQuery();
			new UserinfoDaoImpl();
			List<Task> list =new ArrayList<Task>();
			Task task = null;
			while(rs.next()){
				task = tasks();
				list.add(task);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return null;
	}

	private Task tasks() throws SQLException {
		Task task;
		task = new Task(rs.getInt("tid"), rs.getString("tname"), 
				rs.getString("content"), new Userinfo(rs.getInt("start_uid")), 
				rs.getString("start_time"), rs.getString("state"), 
				new Userinfo(rs.getInt("end_uid")), rs.getInt("task_money"), 
				rs.getString("end_time"), rs.getString("t_img"));
		
		task.setStart_user_tel(rs.getString("start_user_tel"));
		task.setEnd_user_tel(rs.getString("end_user_tel"));
		// 查询发布人、接取人的信息
		UserinfoDao dao = new UserinfoDaoImpl();
		int uid = task.getStart_user().getUid();
		Userinfo userinfo = dao.findById(uid);
		task.setStart_user(userinfo);
		
		
		task.setEnd_user(dao.findById(task.getEnd_user().getUid()));
		
		return task;
	}

	@Override
	public List<Task> find(PageBean pageBean) {
		String sql = "select t.* from task t  "
				+ " order by state, task_money desc,start_time desc   limit ?,?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setInt(1, (pageBean.getCurrentPage()-1)*pageBean.getPageSize());
			pst.setInt(2, pageBean.getPageSize());
			rs=pst.executeQuery();
			new UserinfoDaoImpl();
			List<Task> list =new ArrayList<Task>();
			Task task = null;
			while(rs.next()){
				task = tasks();
				list.add(task);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return null;
	}
	 
	private int findByT_img(Task task){
		String sql ="select tid from task where tname=?  and  content=?  "
				+ "and  start_uid=?  and  start_time=?  and  state=? "
				+ " and task_money=?  ";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, task.getTname());
			pst.setString(2, task.getContent());
			pst.setInt(3, task.getStart_user().getUid());
			pst.setString(4, task.getStart_time());
			pst.setString(5, "0");
			pst.setInt(6, task.getTask_money());
			rs=pst.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return 0;
	}

	@Override
	public String update_jiequ2(Task task) {
		String sql = "update task set end_user_tel=?,state=1,end_uid=?,end_time=? where tid=? ";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, task.getEnd_user_tel());
			pst.setInt(2, task.getEnd_user().getUid());
			pst.setString(3, task.getEnd_time());
			pst.setInt(4, task.getTid());
			int num = pst.executeUpdate();
			if(num>0){
				return task.getTid()+"";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return null;
	}

	@Override
	public int update_fangqi(String tid) {
		String sql = "update task set end_uid=null,end_time=null,"
				+ "end_user_tel=NULL,state=0 where tid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, tid);
			int num = pst.executeUpdate();
			return num;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		
		return 0;
	}

	@Override
	public int update_wancheng(String tid, String end_time) {
		String sql = "update task set end_time=?,state=2 where tid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, end_time);
			pst.setString(2, tid);
			int num = pst.executeUpdate();
			return num;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return 0;
	}

	@Override
	public Task findById(int tid) {
		String sql = "select t.* from task t where tid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setInt(1, tid);
			rs=pst.executeQuery();
			if(rs.next()){
				Task task = new Task(rs.getInt("tid"), rs.getString("tname"), 
						rs.getString("content"), new Userinfo(rs.getInt("start_uid")), 
						rs.getString("start_time"), rs.getString("state"), 
						new Userinfo(rs.getInt("end_uid")), rs.getInt("task_money"), 
						rs.getString("end_time"), rs.getString("t_img"));
				
				task.setStart_user_tel(rs.getString("start_user_tel"));
				task.setEnd_user_tel(rs.getString("end_user_tel"));
				// 查询发布人、接取人的信息
				UserinfoDao dao = new UserinfoDaoImpl();
				int uid = task.getStart_user().getUid();
				Userinfo userinfo = dao.findById(uid);
				task.setStart_user(userinfo);
				
				
				task.setEnd_user(dao.findById(task.getEnd_user().getUid()));
				
				return task;
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
		}
		return null;
	}
}
